/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InterfazInter.java
 *
 * Created on 02-abr-2011, 18:18:29
 */

package cliente;

import Protocolo.ConexionCliente;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author carlosarboleda
 */
public class InterfazConexion extends javax.swing.JDialog {


   



 
     InterfazPrincipal p;



    /** Creates new form InterfazInter */
    public InterfazConexion( InterfazPrincipal principal) {


        initComponents();
        p= principal;

    }



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelConexion = new javax.swing.JPanel();
        labelNombre = new javax.swing.JLabel();
        txtFNombre = new javax.swing.JTextField();
        labelDireccionIp = new javax.swing.JLabel();
        txtFDireccionIp = new javax.swing.JTextField();
        labelPuerto = new javax.swing.JLabel();
        txtFPuerto = new javax.swing.JTextField();
        botonAceptar = new javax.swing.JButton();
        botonCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        panelConexion.setBackground(new java.awt.Color(255, 255, 255));

        labelNombre.setText("Nombre:");

        labelDireccionIp.setText("Ip:");

        labelPuerto.setText("Puerto:");

        botonAceptar.setText("Aceptar");
        botonAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAceptarActionPerformed(evt);
            }
        });

        botonCancelar.setText("Cancelar");
        botonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCancelarActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout panelConexionLayout = new org.jdesktop.layout.GroupLayout(panelConexion);
        panelConexion.setLayout(panelConexionLayout);
        panelConexionLayout.setHorizontalGroup(
            panelConexionLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panelConexionLayout.createSequentialGroup()
                .addContainerGap()
                .add(panelConexionLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(panelConexionLayout.createSequentialGroup()
                        .add(panelConexionLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(labelNombre)
                            .add(labelDireccionIp)
                            .add(labelPuerto))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(panelConexionLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(txtFPuerto)
                            .add(txtFDireccionIp)
                            .add(txtFNombre, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE)))
                    .add(panelConexionLayout.createSequentialGroup()
                        .add(botonAceptar)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(botonCancelar)))
                .addContainerGap(31, Short.MAX_VALUE))
        );
        panelConexionLayout.setVerticalGroup(
            panelConexionLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panelConexionLayout.createSequentialGroup()
                .add(47, 47, 47)
                .add(panelConexionLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(labelNombre)
                    .add(txtFNombre, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(41, 41, 41)
                .add(panelConexionLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(labelDireccionIp)
                    .add(txtFDireccionIp, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(49, 49, 49)
                .add(panelConexionLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(labelPuerto)
                    .add(txtFPuerto, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 51, Short.MAX_VALUE)
                .add(panelConexionLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(botonAceptar)
                    .add(botonCancelar))
                .addContainerGap())
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panelConexion, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panelConexion, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAceptarActionPerformed
        try {
            // TODO add your handling code here:
            p.conectarCliente1(getIp(), getPuerto(), getNombreUsuario());
        } catch (IOException ex) {
            Logger.getLogger(InterfazConexion.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            try {
                p.inicializarCliente();
            } catch (InterruptedException ex) {
                Logger.getLogger(InterfazConexion.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (IOException ex) {
            Logger.getLogger(InterfazConexion.class.getName()).log(Level.SEVERE, null, ex);
        }

        p.deshabilitarMenuConectar();

        p.habilitarMenuDesconectar();

        p.habilitarTxtAreaMensaje();

        

       this.setVisible(false);

       limpiarEspacios();

    }//GEN-LAST:event_botonAceptarActionPerformed

    private void botonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCancelarActionPerformed
        // TODO add your handling code here:

        this.setVisible(false);
    }//GEN-LAST:event_botonCancelarActionPerformed

    /**
    * @param args the command line arguments
    */
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonAceptar;
    private javax.swing.JButton botonCancelar;
    private javax.swing.JLabel labelDireccionIp;
    private javax.swing.JLabel labelNombre;
    private javax.swing.JLabel labelPuerto;
    private javax.swing.JPanel panelConexion;
    private javax.swing.JTextField txtFDireccionIp;
    private javax.swing.JTextField txtFNombre;
    private javax.swing.JTextField txtFPuerto;
    // End of variables declaration//GEN-END:variables


public String getNombreUsuario(){

    return (txtFNombre.getText());


}

public String getIp(){

    return (txtFDireccionIp.getText());


}

public int getPuerto(){

    return (Integer.parseInt(txtFPuerto.getText()));


}


public void limpiarEspacios(){

    txtFNombre.setText("");
    txtFDireccionIp.setText("");
    txtFPuerto.setText("");
}
}
